<!-- 饼图 -->
<template>
  <div>
    <div ref="piechart" :style="{ height: height, width: width }"></div>
  </div>
</template>

<script>
import { param } from '@/utils';
require('echarts/theme/macarons') // echarts theme
export default {
  data() {
    return {
      color: ''
    };
  },
  props: {
    title: {
      type: String,
      default: "综合统计数据分析"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "315px"
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() { },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    this.color = localStorage.getItem("isNightMode")
    this.$nextTick(() => {
      this.setOptions(this.chartData)
    })


  },
  methods: {
    setOptions(chartData) {
      var showData = []
      var sum = 0, max = 0;
      chartData.forEach(item => {
        sum += item.value
        if (item.value >= max) max = item.value
      })
      // 放大规则
      var number = Math.round(max * 0.5)
      showData = chartData.map(item => {
        return {
          value: number + item.value,
          name: item.name
        }
      })
      this.myChart = this.$echarts.init(this.$refs.piechart);
      this.myChart.setOption({
        title: {
          show: false,
          left: "center"
        },
        tooltip: {
          trigger: "item",
          backgroundColor: '#fff',
          formatter: function (param) {
            return param.marker + param.name + '<br/>' + '数量 : ' + (param.value - number)
          }
        },
        legend: {
          show: true,
          top: "bottom",
          type: 'scroll',
          textStyle: { // 新增字体样式配置
            color: this.color === 'night' ? '#fff' : '#000',       // 字体颜色（改成您需要的颜色）
            fontSize: 14,        // 字体大小
            fontFamily: 'Arial'  // 字体类型
          }
          // formatter: function (param){
          //   return param.name + (param.value - number)
          // },
          // data:[param]
        },
        series: [
          {
            type: "pie",
            roseType: 'radius',
            radius: [20, 100],
            data: showData,
            width: '100%', // 强制宽度占满容器
            height: '100%', // 强制高度占满容器
            label: {
              normal: {
                show: true,
                formatter: function (param) {
                  return param.name + ' (' + (param.value - number) + ')'
                }
              }
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0
              }
            }
          }
        ]
      });
    }
  }
};
</script>
<style scoped></style>
